package JtableDemo;

import Dao.DbConnection;

import java.awt.*;
import java.sql.*;
import java.util.*;
import java.util.concurrent.Executor;
import javax.swing.*;
import javax.swing.border.*;
/*
 * Created by JFormDesigner on Fri May 26 23:45:41 CST 2023
 */



/**
 * @author wxz
 */
public class Jdbcdemo extends JFrame {
    public Jdbcdemo() throws SQLException {
        initComponents();
    }

    private void initComponents() throws SQLException {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        //数据库配置
        Connection conn = null;
        PreparedStatement pstm = null;
//		Statement stmt;
        ResultSet rs = null;

        conn = DbConnection.getConnection();
        String sql = "select * from powerprice";
        assert conn != null;
          pstm = conn.prepareStatement(sql);
         rs = pstm.executeQuery();
        int count = 0;
        while (rs.next()) {
            count++;
        }
        rs = pstm.executeQuery();
        // 将查询获得的记录数据，转换成适合生成JTable的数据形式
        Object[][] info = new Object[count][3];
        String[] title = {"ID","type","price"};
        count = 0;
        while (rs.next()) {
            info[count][0] = Integer.valueOf(rs.getInt("powerId"));
            info[count][1] = rs.getString("powerName");
            info[count][2] = Integer.valueOf(rs.getInt("powerPrice"));

            count++;
        }
        table2 = new JTable(info ,title);

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(table2);
        }
        contentPane.add(scrollPane1, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable table2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
